const onlyNumber = {
  inserted: function(el, binding, vnode) {
    const inputElement = el.querySelector('input')
    if (!inputElement) return
    inputElement.onkeyup = function(e) {
      inputElement.value = inputElement.value.replace(/[^0-9.]/g, '')
      // 更新Vue响应式
      if (vnode.componentInstance) {
        vnode.componentInstance.$emit('input', inputElement.value)
      } else {
        vnode.elm.dispatchEvent(new CustomEvent('input', inputElement.value))
      }
    }
    inputElement.onblur = function(e) {
      inputElement.value = inputElement.value.replace(/[^0-9.]/g, '')
      if (vnode.componentInstance) {
        vnode.componentInstance.$emit('input', inputElement.value)
      } else {
        vnode.elm.dispatchEvent(new CustomEvent('input', inputElement.value))
      }
    }
    el.__vueOnlyNumberHandler__ = inputElement
  },
  // unbind 钩子中移除事件监听器
  unbind: function(el) {
    if (el.__vueOnlyNumberHandler__) {
      el.removeEventListener('input', el.__vueOnlyNumberHandler__)
      delete el.__vueOnlyNumberHandler__ // 清理引用
    }
  }
}
export default onlyNumber
